package com.zhengtoon.mctoon.conv.service;

import com.alibaba.fastjson.JSON;
import com.bjtoon.framework.utils.redis.RedisCacheService;
import com.zhengtoon.mctoon.conv.bean.session.OrgSessionInfo;
import com.zhengtoon.mctoon.conv.constant.OrgSessionConstant;
import com.zhengtoon.mctoon.conv.service.IOrgSessionService;
import com.zhengtoon.mctoon.conv.utils.AppConstant;
import com.zhengtoon.mctoon.conv.utils.PlatformSessionUtils;
import com.zhengtoon.mctoon.conv.utils.SysConfig;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * PC端session 服务
 *
 * @author 144637
 * @since 2017-7-17
 */
@Service
public class OrgSessionServiceImpl implements IOrgSessionService {

    private static Logger log = LoggerFactory.getLogger(OrgSessionServiceImpl.class);

    @Autowired
    private RedisCacheService redisCacheService;

    // session 过期时间(分钟)
    private final static Long SESSION_EXPIRED_MINUTES = Long
            .valueOf(SysConfig.getPropertyValue("config.session_expired_minutes"));

    @Override
    public OrgSessionInfo getOrgSessionInfo(String sessionId) {

        String sessionKeyWithPrefix = getOrgSessionKeyWithPrefix(sessionId);
        String sessionJsonString = getSessionStringFromRedis(sessionKeyWithPrefix);
        OrgSessionInfo orgSessionInfo = getSessionObjectFromJsonString(sessionJsonString);


        putOrgSessionInfoToRedis(sessionKeyWithPrefix, sessionJsonString);
        return orgSessionInfo;
    }

    @Override
    public String getSessionByUserToken(String userToken) {

        String sessionId = UUID.randomUUID().toString();
        OrgSessionInfo orgSessionInfo = getSessionInfoFromPlatformAPI(userToken);
        String sessionKeyWithPrefix = getOrgSessionKeyWithPrefix(sessionId);
        String sessionJsonString = getJSONStringFromSessionObject(orgSessionInfo);

        putOrgSessionInfoToRedis(sessionKeyWithPrefix, sessionJsonString);
        return sessionId;
    }

    /**
     * session对象==>json String
     *
     * @param orgSessionInfo org 会话对象
     * @return String
     * @author 144637
     */
    private String getJSONStringFromSessionObject(OrgSessionInfo orgSessionInfo) {
        return JSON.toJSONString(orgSessionInfo);
    }

    /**
     * 从平台获取会话信息
     *
     * @param userToken 用户令牌
     * @return OrgSessionInfo
     */
    private OrgSessionInfo getSessionInfoFromPlatformAPI(String userToken) {
        return PlatformSessionUtils.validateToken(userToken);
    }

    /**
     * json String ==> 会话对象
     *
     * @param sessionJsonString JSON string
     * @return OrgSessionInfo
     * @author 144637
     */
    private OrgSessionInfo getSessionObjectFromJsonString(String sessionJsonString) {
        return JSON.parseObject(sessionJsonString, OrgSessionInfo.class);
    }

    /**
     * 从redis中获取会话的jsonString
     *
     * @param orgSessionKey 带前缀的sessionId
     * @return String
     * @author 144637
     */
    private String getSessionStringFromRedis(String orgSessionKey) {
        return redisCacheService.getRedisCacheByKey(orgSessionKey);
    }

    /**
     * 将会话信息对象string放入redis
     *
     * @param sessionKeyWithPrefix 带前缀的sessionId
     * @param sessionJsonString    orgSessionInfo转成的JSON String
     * @author 144637
     */
    private void putOrgSessionInfoToRedis(String sessionKeyWithPrefix, String sessionJsonString) {
        redisCacheService.setRedisCacheByKey(sessionKeyWithPrefix, sessionJsonString,
                SESSION_EXPIRED_MINUTES, AppConstant.CONV_REDIS_CACHE_TIME_UNIT);
    }

    /**
     * 获取PC端后台session前缀
     *
     * @param sessionId 不带前缀的sessionId
     * @return String
     * @author 144637
     */
    private String getOrgSessionKeyWithPrefix(String sessionId) {
        return OrgSessionConstant.ORG_SESSION_PREFIX + sessionId;
    }

}
